define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap"
],function($,IO,bootbox,bootstrap){
    var user_id;
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '编号',
                dataIndex: 'user_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_id+"</span>";
                }
            },
            {
                title: '姓名',
                dataIndex: 'user_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_name+"</span>";
                }
            },
            {
                title: '用户积分',
                dataIndex: 'user_integral',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_integral+"</span>";
                }
            },

            {
                title: '排名',
                dataIndex: 'sequence',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].sequence+"</span>";
                }
            },

            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='#' class='show' style='color: green;' user_id='"+data[datarow].user_id+"'>展示详情</a></span>";
                }
            },

        ];


        //数组初始化
        function dataInit() {
            grid = new Grid("/overviewsList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            return "listType=0";
        }
        dataInit();

    });
    /**---------------------------------获取展示详情--------------------------------*/
    $(document).on("click",".show",function(){

        var param={};
        user_id=$(this).attr('user_id');
        param.user_id=user_id;
        IO.get('/overviewsShow',param,function (d) {
            var dataAll=d.data.trains;
            console.log(d.data)
            var dataCount=dataAll.length;
            $('#tbody').empty();
            $('#user_name').text(d.data.user_name);
            $('#sequence').text(d.data.sequence);
            $('#user_integral').text(d.data.user_integral);

            for(var i=0;i<dataCount;i++){
                $('#tbody').append('<tr>'+
                    '<td id="train_name"></td>'+
                    '<td id="total_score"></td>'+
                    '<td id="study_result"></td>'+
                    // '<td id="study_report"></td>'+
                    '<td id="exam_result"><input style="height:44px;width: 100%; border: 0px solid grey;text-align:center;"  name=" " type="text" /></td>'+
                    '<td id="btn"><button train_id="0" class="btn btn_update" style="background-color: transparent;color: green;">编辑</button></td>'+
                    '</tr>');
                $('#tbody').children().last().find('#train_name').text(dataAll[i].train_name)
                $('#tbody').children().last().find('#study_result').text(dataAll[i].study_result)
                $('#tbody').children().last().find('#total_score').text((dataAll[i].study_result*0.8+dataAll[i].exam_result*0.2).toFixed(2));
                $('#tbody').children().last().find('#exam_result').find('input').val(dataAll[i].exam_result)
                $('#tbody').children().last().find('#btn').find('.btn_update').attr('train_id',dataAll[i].train_id);
            }
           $('#tbody').children().first().find('input').focus();
          // $('#tbody').children().first().find('input').focusin(function () {
          //     $(this).css('background-color','red');
          // });
})
       $('#myModal').modal('show');
    });

    //点击编辑
    $(document).on("click",".btn_update",function(){
        var  train_id=$(this).attr('train_id');
        var self=$(this);
        var  exam_result= $(this).parents('tr').find('input').val();
        if(exam_result==""){
            bootbox.alert('请输入该课程考核成绩');
            return;
        }

      if(!(new RegExp(/^\d+(\.\d+)?$/)).test(exam_result)){
          bootbox.alert('请输入正确格式的成绩');
          return;
      }
        var param={};
        param.user_id=user_id;
        param.train_id=train_id;
        param.exam_result=exam_result;
        IO.get('/reportseeExamUpdate',param,function (d) {
            if(d.code==0){
                bootbox.alert(d.msg,function () {
                    var total_score= self.parents('tr').find('#total_score');
                    total_score.text((self.parents('tr').find('#study_result').text()*0.8+self.parents('tr').find('#exam_result').find('input').val()*0.2).toFixed(2));
                });
            }else{
                bootbox.alert(d.msg);
            }
        })
    


    })
    
 
})